<template>
  <el-dialog title="关联权限" :visible.sync="value" :before-close="cancel">
    <el-scrollbar style="height:100%">
      <el-tree
        ref="tree"
        :default-checked-keys="checkedMenus"
        :default-expanded-keys="checkedMenus"
        show-checkbox
        :check-strictly="true"
        node-key="id"
        :data="menus"
        :props="defaultProps"
      ></el-tree>
    </el-scrollbar>

    <div slot="footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { list } from "@/api/menu";
import { relativeMenus, bindingMenus } from "@/api/role";
export default {
  name: "relativeIndex",
  components: {},
  props: {
    value: Boolean
  },
  data() {
    return {
      roleId: null,
      menus: [],
      checkedMenus: [],
      defaultProps: {
        children: "children",
        label: "name"
      }
    };
  },
  methods: {
    async init(roleId) {
      this.roleId = roleId;
      const resp = await relativeMenus(roleId);
      this.checkedMenus = resp.data;
      const { data } = await list(this.searchForm);
      this.menus = data;
    },
    async submit() {
      const nodes = this.$refs["tree"].getCheckedNodes();
      const menuIds = nodes.map(item => item.id);
      await bindingMenus(this.roleId, menuIds);
      this.cancel();
    },
    cancel() {
      this.$emit("input", false);
    }
  }
};
</script>

<style lang="scss">
.el-dialog__body {
  height: 400px;
}
</style>